package com.cysion.study.ui.fragment

import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import com.cysion.other.startActivity_ex
import com.cysion.study.R
import com.cysion.study.adapter.PendingsAdapter
import com.cysion.study.entity.ApprovalEntity
import com.cysion.study.entity.ResearchStuEntity
import com.cysion.study.helper.UserCache
import com.cysion.study.ktbox.base.BaseFragment
import com.cysion.study.ktbox.base.ITEM_CLICK
import com.cysion.study.ktbox.net.BaseResponseRx
import com.cysion.study.ktbox.net.ErrorHandler
import com.cysion.study.net.ApiClient
import com.cysion.study.uibox.dialog.Alert
import com.cysion.study.widget.ShowToast
import com.cysion.targetfun._subscribe
import com.scwang.smartrefresh.layout.constant.RefreshState
import kotlinx.android.synthetic.main.fragment_pending.*
import kotlinx.android.synthetic.main.fragment_pending.rv_list
import kotlinx.android.synthetic.main.fragment_pending.smartLayout

//待审批
class PendingsFragment : BaseFragment() {




//    private var mdatalist: MutableList<String> = mutableListOf()
    private lateinit var pendingAdapter: PendingsAdapter
    private var curPage = 1
    private var category = 0//0.待我审核 1.抄送我的
    override fun getLayoutId(): Int = R.layout.fragment_pending

    override fun initView() {
        initRefreshLayout()
        initRecyclerView()
    }

    override fun initData() {
        super.initData()
        smartLayout.autoRefresh()
//        smartLayout.finishRefresh()
    }

    //    初始化刷新控件
    private fun initRefreshLayout() {
        smartLayout.setOnRefreshListener {
            curPage = 1
            getDataList()
            smartLayout.setEnableLoadMore(true)

        }
        smartLayout.setOnLoadMoreListener {
            getDataList()
        }
    }

    //初始化列表
    private fun initRecyclerView() {
        rv_list.isNestedScrollingEnabled = false
        pendingAdapter = PendingsAdapter(dataList, context)
        rv_list.adapter = pendingAdapter
        rv_list.layoutManager = LinearLayoutManager(context)
        pendingAdapter.setOnTypeClickListener { obj, position, flag ->
            if (flag == ITEM_CLICK) {//审批详情
                var bundle = Bundle()
                bundle.putString("type","1")//type 1 职位入口进详情 2 我的应聘入口进详情
                context.startActivity_ex<PendingActivity>("key", bundle)
            }
        }
        //模拟数据
        /*if (pendingAdapter.itemCount == 0) {
            var lists: MutableList<String> = mutableListOf("","")
            mdatalist.addAll(lists)
            pendingAdapter.notifyDataSetChanged()
        }*/
    }

    private val dataList: MutableList<ApprovalEntity> = mutableListOf()

    private fun getDataList() {
        Alert.loading(context)
        ApiClient.api.getOwnDealedList(UserCache.token,curPage).compose(BaseResponseRx.validateToMain())
            ._subscribe {
                _onNext {
                    Alert.close()
                    stopRefresh()
                    if (curPage == 1) {
                        dataList.clear()
                        if (it.size == 0) {
                            multiView.showEmpty()
                        }
                    }
                    if (it.size <= 0) {
                        smartLayout.setEnableLoadMore(false)
                    } else {
                        curPage++
                        dataList.addAll(it)
                        pendingAdapter.notifyDataSetChanged()
                        multiView.showContent()
                    }
                }
                _onError {
                    Alert.close()
                    ShowToast(ErrorHandler.handle(it).errorMsg)
                    stopRefresh()
                }
            }
    }
    private fun stopRefresh() {
        if (smartLayout.state == RefreshState.Refreshing) {
            smartLayout.finishRefresh()
        } else if (smartLayout.state == RefreshState.Loading) {
            smartLayout.finishLoadMore(100)
        }
    }


    private fun noticeRead(tid: String, remark: String, flag:Int) {
        // flag1.同意 2.拒绝
        ApiClient.api.approvalDeal(
            UserCache.token, tid, remark,flag.toString()
        ).compose(BaseResponseRx.validateToMain())
            ._subscribe {
                _onNext {
                    ShowToast("操作成功")
//                    presenter.getList(curPage, 0)
                    Alert.close()
                }
                _onError {
                    //                        Alert.close()
                    ShowToast(ErrorHandler.handle(it).errorMsg)
                }
            }
    }



    override fun closeMvp() {
    }

}
